import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/emergency_event/emergency_event_api.dart';
import 'package:ls_work/emergency_event/response/emergency_event_detail_data.dart';
import 'package:ls_work/emergency_event/response/emergency_event_list_data.dart';
import 'package:ls_work/emergency_event/response/emergency_event_type_data.dart';

class EmergencyEventModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  ///紧急事件类型
  Future<List<EmergencyEventTypeData>> emergencyEventType() async {
    var data =
        await HttpGo.instance.postData(EmergencyEventApi.emergencyEventType);
    if ("0" == data['code']) {
      return ((data['data']['codeValueList'] as List?) ?? [])
          .map((e) => EmergencyEventTypeData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///紧急事件记录
  Future<List<EmergencyEventListData>> emergencyEventRecord(
      {dynamic eventNature,
      dynamic eventType,
      int page = 1,
      int rows = 20,
      String? stratDateLike,
      bool wrapper = true}) async {
    var map = {
      "eventNature": eventNature,
      "eventType": eventType,
      "page": page,
      "rows": rows,
      "stratDateLike": stratDateLike,
      "wrapper": wrapper,
    };
    var data = await HttpGo.instance.postData(
        EmergencyEventApi.emergencyEventRecord,
        data: map,
        options: Options(contentType: Headers.jsonContentType));
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => EmergencyEventListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///事件详情
  Future<EmergencyEventDetailData> eventDetail(dynamic id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(EmergencyEventApi.eventDetail,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return EmergencyEventDetailData.fromJson(data['data']);
    } else {
      throw data['msg'];
    }
  }

  ///事件提交
  Future<Map> submitEvent(dynamic map) async {
    var data = await HttpGo.instance.postData(EmergencyEventApi.submitEvent,
        data: map,
        options: Options(contentType: Headers.jsonContentType));
    if('0'==data['code']){
      return data;
    }else{
      throw data['msg'];
    }
  }
}
